package com.sjc.rpcdemo

import org.apache.spark.{SparkConf, SparkContext, SparkEnv}
import org.apache.spark.rpc.{RpcEndpoint, RpcEnv}
import org.apache.spark.sql.SparkSession

/**
 *  Spark RPC 服务端
 */
object RpcServerMain {

  def main(args: Array[String]): Unit = {

    // 获取 SparkEnv
    val conf: SparkConf = new SparkConf()
    val sparkSession = SparkSession.builder().config(conf).master("local[*]").appName("NX RPC").getOrCreate()
    val sparkContext: SparkContext = sparkSession.sparkContext
    val sparkEnv: SparkEnv = sparkContext.env

    // 构建 RpcEnv
    val rpcEnv = RpcEnv
      .create(HelloRpcSettings.getName(), HelloRpcSettings.getHostname(), HelloRpcSettings.getHostname(), HelloRpcSettings.getPort(), conf,
        sparkEnv.securityManager, 1, false)

    // 创建 和启动 endpoint
    val helloEndpoint: RpcEndpoint = new HelloEndPoint(rpcEnv)
    //  通过 rpcEnv 的 setupEndpoint 方法来启动 RpcEndpoint
    //  rpcEnv = actorySystem,  setupEndpoint = actorOf,  helloEndpoint = actor
    rpcEnv.setupEndpoint(HelloRpcSettings.getName(), helloEndpoint)

    rpcEnv.awaitTermination()
  }
}
